<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="name here">
    <meta name="author" content="name here">	
	
	 <title>Password Change</title>
	 
	<link rel="stylesheet" href="css/jquery.mobile.structure-1.3.0.css">
	<link rel="stylesheet" href="themes/BancoDelPeru.css">
	
	<script src="js/jquery-1.9.1.js"></script>
	<script src="js/jquery.mobile-1.3.0.js"></script>
	<script type="text/javascript" src="js/cordova-2.5.0.js" charset="utf-8" ></script>
	<script src="js/configs.js"></script>
	
	
	<script type="text/javascript" charset="utf-8">
		function iniciar() {			
			document.addEventListener("deviceready", dispositivoListo, false);			
		}
		function dispositivoListo() {
				
		}
		
		function changePassword(){
		    var cust = $("#customerID").val();
		    var pass = $("#customerPassword").val();
		    var changed = $("#newPassword").val();
		    String change = $("#newPassword");
		    var verified = $("#verifynewpass").val();
		    String verify = $("#verifynewpass");
		    
		    if(! change.equals(verify))
		    	{
		    		alert("New password does not match");
		    		window.location.assign("changepassword.html");
		    	}
		    
		    else{
		    	
		    	$.post("http://"+SERVER_IP_ADDRESS+":8080/WebBancoPeru/rest/customer/login", {customerID: cust, customerPassword: pass}, function(data){
		    		
		    		if ( "valid" == data.status ){
		    			
		    			$.post("http://"+SERVER_IP_ADDRESS+"/WebBancoPeru/rest/customer/updatePassword", {customerID: cust, customerPassword: pass, newPassword: changed}, function(data){
		    				
		    				if("valid" == data.status){
		    					alert("Password changed successfully!");
		    					window.location.assign("login.html");
		    				}
		    				
		    				else{
		    					alert("Error");
		    					window.location.assign("changepassword.html");
		    				}
		    				
		    			}, "json");
		    		}
		    		
		    		else{
		    			alert("Error");
		    			window.location.assign("changepassword.html");
		    		}
		    	}, "json");
		    }
		    
		}
		
</script>
	
</head>
<body onload="iniciar()">

	<div data-role="page">
		<div data-role="header" data-position="fixed">		
			<h1>Banco Per&uacute;</h1>
		</div>
		
		<div data-role="content">
			<label for="text-basic">CustomerID:</label>
			<input type="text" name="username" id="customerID" class="required" />
			
			<label for="text-basic">Current Password:</label>
			<input type="password" name="Current Password" id="customerPassword" class="required" />

			<label for="text-basic">New Password:</label>
			<input type="password" name="New Password" id="newpass" class="required" />
			
			<label for="text-basic">Verify Password:</label>
			<input type="password" name="Verify Password" id="verifynewpass" class="required" />
			
			<a data-role="button" data-inline="true" href="login.html">Go back!!</a>
			<a type="button" data-inline="true" data-align="true" onclick="changePassword()"> Save Password</a>

		</div>
		<div data-role="footer" data-position="fixed">
			<h1>Banco Per&uacute; &copy; 2013</h1>
		</div>
	</div>
	
</body>

</html>